﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2011
// admin@franknagl.de
//
namespace SBIP.OptionsForms.NonSBIP
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;
    using Filter;
    using Filter.NonSBIP;

    /// <summary>Filter specified properties form.</summary>
    public partial class NSColorEliminatedRegionGrowingForm : Form
    {
        private readonly Processor processor;
        private readonly NSColorEliminatedRegionGrowing nsfilter;

        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="NSColorEliminatedRegionGrowingForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public NSColorEliminatedRegionGrowingForm(Processor processor)
        {
            InitializeComponent();

            this.processor = processor;
            nsfilter = new NSColorEliminatedRegionGrowing();
            nsfilter.EliminateColor = Color.FromArgb(255, 255, 255);            
            processor.Filter = new Original();
        }

        private void BtnColorChangeClick(object sender, EventArgs e)
        {
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                nsfilter.EliminateColor = colorDialog.Color;
                btnColorChange.BackColor = colorDialog.Color;
                lbEliminateColor.Text = string.Format(
                    "Red: {0}   Green: {1}   Blue: {2}",
                    colorDialog.Color.R,
                    colorDialog.Color.G,
                    colorDialog.Color.B);
            }
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {            
            //EndlessProgressBarFormInThread form = 
            //    new EndlessProgressBarFormInThread(
            //        "Do objects extracting ... ", 
            //        "Please wait, this takes a few seconds.");
            //form.Start();
            try
            {
                Bitmap bitmap = 
                    processor.OriginalBitmap.PixelFormat == PixelFormat.Format32bppArgb ||
                    processor.OriginalBitmap.PixelFormat == PixelFormat.Format32bppRgb
                     ? Helper.BitmapConverter.ARGBToRGB(processor.OriginalBitmap)
                     : processor.OriginalBitmap;

                bitmap = nsfilter.Apply(bitmap);
                processor.ChangeTexture(bitmap);
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            // lbContourNumbers.Text = canny.Contours.Count.ToString();

            // form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            processor.ResetTexture();
        }
    }
}
